package com.mlizhi.widgets.cursor4water;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mlizhi.kingdom.R;

public class CursorView extends RelativeLayout {
	
	private float mProgress;
	private float bgViewWidth = 0f;
	private float bgTextSize = 30.0f;
	private int cursorTextColor;
	private TextView cursorText;
	
	//背景区间中的文字样式
	private TextPaint tp ;

	public CursorView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public CursorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public CursorView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initCursorView();
	}
	
	
	
	/**
	 * 初始化组件
	 */
	public void initCursorView(){
		RelativeLayout.LayoutParams param4cursor =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		param4cursor.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
		param4cursor.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		param4cursor.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		//干燥区间
		cursorText = new TextView(getContext());
		cursorText.setGravity(Gravity.CENTER);
		cursorText.setText("0.0%");
		cursorText.setTextColor(Color.WHITE);
		tp = cursorText.getPaint(); 
		addView(cursorText,param4cursor);
		
	}
	
	
	public int getCursorHeight(){
		
		
		FontMetrics fm = tp.getFontMetrics();
		int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 10); 
		
		return textHeight;
	}
	
	
	

	public float getmProgress() {
		return mProgress;
	}

	/**
	 * 本次测试，自动滑到指定位置
	 * @param mProgress
	 * @param minScale
	 * @param maxScale
	 */
	public void setmProgress(float mProgress,float minScale,float maxScale) {
		this.mProgress = mProgress;
		String str = String.valueOf(mProgress+"%");
		cursorText.setText(str);
//		cursorText.setTextColor(cursorTextColor);
		cursorText.setBackgroundResource(R.drawable.ic_cursor_bg);
		ViewGroup.LayoutParams param4cursor = cursorText.getLayoutParams();
		int leftMargin = (int) (bgViewWidth * ((mProgress-minScale)/(maxScale-minScale)) - tp.measureText(str) * 0.5f);
		if (param4cursor != null) {
			((LayoutParams) param4cursor).leftMargin = leftMargin;
			
		}
		TranslateAnimation translate = new TranslateAnimation(-leftMargin,0, 0, 0);
		translate.setDuration(1000);
		translate.setInterpolator(new LinearInterpolator());
		translate.setFillAfter(true);
		cursorText.startAnimation(translate);
		
	}

	public float getBgViewWidth() {
		return bgViewWidth;
	}

	public void setBgViewWidth(float bgViewWidth) {
		this.bgViewWidth = bgViewWidth;
	}

	public float getBgTextSize() {
		return bgTextSize;
	}

	public void setBgTextSize(float bgTextSize) {
		this.bgTextSize = bgTextSize;
	}

	public int getCursorTextColor() {
		return cursorTextColor;
	}

	public void setCursorTextColor(int cursorTextColor) {
		this.cursorTextColor = cursorTextColor;
	}

	
	
	
	
}
